﻿using System;
using System.Collections.Generic;
using Engine;
using Game;
using Mekiasm.Common;

namespace Mekiasm
{
    public class CropCron : MachineRotateAbleItem
    {
        public static int Index;

        public CropCron() : base("作物监管机")
        {

            textureSlots = new int[] { 128, 128, 128, 128, 129, 130 };
        }
        public override MekBlock.DeviceType GetDeviceType()
        {
            return MekBlock.DeviceType.Electric;
        }

        public override bool IsInteractive(SubsystemTerrain subsystemTerrain, int value)
        {
            return true;
        }
        public override void OnBlockAdded(SubsystemBlockBehavior blockBehavior, int value, int oldValue, int x, int y, int z)
        {
            List<ComponentEnergyMachine.Slot> slots = new List<ComponentEnergyMachine.Slot>();
            for (int i = 0; i < 20; i++)
            {
                if (i == 16)
                {
                    slots.Add(new ComponentEnergyMachine.Slot() { FuelCapacity = 10240 });
                }
                else
                {
                    slots.Add(new ComponentEnergyMachine.Slot());
                }
            }
            ILibrary.AddBaseEnergyEntity(DisplayName, x, y, z, slots,
                ILibrary.GetRotetedConfig(value, new MachineFaceConfig[] {
            new MachineFaceConfig(MekBlock.MachineMode.IN,MekBlock.DeviceType.Electric),
            new MachineFaceConfig(MekBlock.MachineMode.IN,MekBlock.DeviceType.Fluid),
            new MachineFaceConfig(MekBlock.MachineMode.IN,MekBlock.DeviceType.Electric),
            new MachineFaceConfig(MekBlock.MachineMode.IN,MekBlock.DeviceType.Fluid),
            new MachineFaceConfig(MekBlock.MachineMode.OUT,MekBlock.DeviceType.Item),
            new MachineFaceConfig(MekBlock.MachineMode.OUT,MekBlock.DeviceType.Item)
            }), new MachineAmountLimitConfig() { InputPowerLimit = 32, OutputPowerLimit = 32, MaxPower = 10240 ,InputFluidAmountLimit = 32});
            ElectricSystem.Add(new CropCronElement(x, y, z));
            FluidSystem.Add(new FluidElement.CropCronFluidElement(x, y, z));
        }

        public override void OnBlockGenerated(int value, int x, int y, int z, bool isLoaded)
        {
            ElectricSystem.Add(new CropCronElement(x, y, z));
            FluidSystem.Add(new FluidElement.CropCronFluidElement(x, y, z));
        }
        public override void OnBlockRemoved(SubsystemBlockBehavior blockBehavior, int value, int newValue, int x, int y, int z)
        {
            ElectricSystem.Remove(new Point3(x, y, z));
            FluidSystem.Remove(new Point3(x, y, z));
            ILibrary.RemoveBlockEntity(x, y, z);
        }
        public override bool OnInteract(SubsystemBlockBehavior blockBehavior, TerrainRaycastResult raycastResult, ComponentMiner componentMiner)
        {
            ComponentEnergyMachine mcomponent = blockBehavior.Project.FindSubsystem<SubsystemBlockEntities>().GetBlockEntity(raycastResult.CellFace.X, raycastResult.CellFace.Y, raycastResult.CellFace.Z).Entity.FindComponent<ComponentEnergyMachine>(true);
            componentMiner.ComponentPlayer.ComponentGui.ModalPanelWidget = new CropCronWidget(mcomponent, componentMiner);
            return true;
        }

        public override BlockPlacementData GetPlacementValue(SubsystemTerrain subsystemTerrain, ComponentMiner componentMiner, int value, TerrainRaycastResult raycastResult)
        {
            return ILibrary.GetPlacementValue(subsystemTerrain, componentMiner, value, raycastResult);
        }
    }
}
